package com.thread.www;


import java.util.concurrent.CountDownLatch;

/**
 * Created by 27340 on 2017/3/26.
 */
public class CountDowLawtchTest{
    public static void main(String[] args){
        try {
            CountDownLatch latch = new CountDownLatch(3);
            Worker worker1 = new Worker("jack1" , latch);
            Worker worker2 = new Worker("jack2" , latch);
            Worker worker3 = new Worker("jack3" , latch);
            worker1.start();
            worker2.start();
            worker3.start();
            latch.await();
            System.out.println("main thread is end");
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    static class Worker extends Thread{
        private String workerName;
        private CountDownLatch latch;
        public Worker(String workerName , CountDownLatch latch){
            this.workerName = workerName;
            this.latch = latch;
        }
        @Override
        public void run(){
            try {
                System.out.println("workerName "+ workerName +" is begin");
                Thread.sleep(1000);
                System.out.println("workerName " + workerName + " is end");
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            latch.countDown();
        }
    }
}
